package com.pine.dcts.model;

import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.annotation.TableId;
import com.baomidou.mybatisplus.annotation.TableName;

import com.pine.dcts.entity.BaseEntity;
import lombok.Data;

import java.beans.Transient;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.Objects;

/**
 * 表
 * 层级结构：
 * datasource
 * catalog
 * schema
 * table
 * 不同类型的数据源，并不一定都有 catalog,schema
 *
 * @see java.sql.DatabaseMetaData getTables
 */

@Data
@TableName("dep_table")
public class Table  extends BaseEntity implements Comparable<Table>{
    /**
     * 表id
     */

    @TableId(value = "id", type = IdType.AUTO)
    private Integer tableId;

    /**
     * 数据源id
     */

    @TableField("ds_id")
    private Integer datasourceId;

    /**
     * catalog
     * TABLE_CAT
     */

    @TableField("catalog_name")
    private String catalogName;
    /**
     * schema
     * TABLE_SCHEM
     */

    @TableField("schema_name")
    private String schemaName;
    /**
     * 表名
     * TABLE_NAME
     */

    @TableField("table_name")
    private String tableName;



    /**
     * 表全名
     * schema.catalog.table
     */
    @TableField("tb_fullname")
    private String tableFullName;


    /**
     * 表类型
     * TABLE_TYPE
     * "TABLE", "VIEW", "SYSTEM TABLE", "GLOBAL TEMPORARY",
     * "LOCAL TEMPORARY", "ALIAS", "SYNONYM"
     * "表类型: TABLE-普通表,VIEW-视图,SYSTEM TABLE-系统表,GLOBAL TEMPORARY-全局临时表,LOCAL TEMPORARY-本地临时表,ALIAS-别名,SYNONYM-同义词"
     */
    @TableField("table_type")
    private String tableType;
    /**
     * 数据库里定义的表注释
     * REMARKS
     */
    @TableField("table_comment")
    private String tableComment;
    /**
     * 表的行数
     */
    @TableField("table_count")
    private Long rowCount;
    /**
     * 备注(用户自填)
     */
    @TableField("remark")
    private String remark;

    /**
     * 获取表行数的花费时间(s)
     * select count(*) from table
     */
    @TableField("table_count_time")
    private Integer tableCountTime;

    /**
     * 元数据更新时间 UTC时间戳
     */
    @TableField("update_time")
    private Date updateTime;

    /**
     * 表字段
     */
    @TableField(exist = false)
    private List<Field> fields;

    /**
     * 索引
     */
    @TableField(exist = false)
    private List<Index> indexes;

    @TableField(exist =false)
    private Map<String,Field> fieldsMap;

    @Override
    public int compareTo(Table o)
    {
        long count1 = (rowCount == null?0:rowCount);
        long count2 = (o.rowCount == null?0:o.rowCount);
        return Long.compare(count1, count2);
    }
}
